import {
  getSchoolAPI,
  getUserInfoAPI,
  getQiniuToken,
} from '@/api/base/base_api'
import store from '@/store'

const state = () => ({
  schoolData: [],
  qiniuToken: '',
  sign: false,
  userInfo: {},
  shopAuditData: [
    { value: 1, label: '扭转成待审核' },
    { value: 2, label: '审核通过(上架)' },
    { value: 3, label: '审核未通过' },
    { value: 4, label: '下架' },
    { value: 5, label: '删除' },
  ],
  shopAuditYesOrNoData: [
    { value: 1, label: '通过' },
    { value: 2, label: '不通过' },
  ],
  userStatusData: [
    { value: 0, label: '全部' },
    { value: 1, label: '未认证' },
    { value: 2, label: '认证中' },
    { value: 3, label: '已认证' },
    { value: 4, label: '审核失败' },
    { value: 5, label: '封禁' },
  ],
  userAccountData: [
    { value: 1, label: '下线' },
    { value: 2, label: '封禁' },
    { value: 3, label: '删除' },
    { value: 4, label: '恢复正常' },
  ],
  userTypeData: [
    { value: 0, label: '无认证人员' },
    { value: 1, label: '工作人员' },
    { value: 2, label: '学生' },
    { value: 3, label: '老师' },
    { value: 4, label: '商家' },
  ],
  roleData: [
    { value: 1, label: '超级管理员' },
    { value: 2, label: '普通管理员' },
    { value: 3, label: '校区管理员' },
    { value: 4, label: '商家角色' },
    { value: 5, label: '其他' },
  ],
  schoolStatusData: [
    { value: 1, label: '待审核' },
    { value: 2, label: '正常' },
    { value: 3, label: '下线' },
  ],
  shopStatusData: [
    { value: 1, label: '待审核' },
    { value: 2, label: '审核通过' },
    { value: 3, label: '审核未通过' },
    { value: 4, label: '申请上架中商铺' },
    { value: 5, label: '上架审核失败商铺' },
    { value: 6, label: '已上架商铺' },
    { value: 7, label: '已下架商铺' },
  ],
  yesOrNoData: [
    { value: 1, label: '是' },
    { value: 2, label: '否' },
  ],
})
const getters = {
  schoolData: (state) => state.schoolData,
  shopStatusData: (state) => state.shopStatusData,
  userInfo: (state) => state.userInfo,
  userStatusData: (state) => state.userStatusData,
  userTypeData: (state) => state.userTypeData,
  schoolStatusData: (state) => state.schoolStatusData,
  roleData: (state) => state.roleData,
  userAccountData: (state) => state.userAccountData,
  sign: (state) => state.sign,
  qiniuToken: (state) => state.qiniuToken,
  yesOrNoData: (state) => state.yesOrNoData,
  shopAuditData: (state) => state.shopAuditData,
  shopAuditYesOrNoData: (state) => state.shopAuditYesOrNoData,
}
const mutations = {
  setSchoolData(state, data) {
    state.schoolData = data
  },
  setUserIfno(state, info) {
    state.userInfo = info
  },
  SET_SIGN: (state, sign) => {
    state.sign = sign
  },
  QINIU_TOKEN: (state, data) => {
    state.qiniuToken = data
  },
}
const actions = {
  async getSchoolData({ commit }, user) {
    if (user.roleId >= 3) {
      let data = [
        {
          schoolId: user.schoolId,
          schoolName: user.schoolName,
        },
      ]
      commit('setSchoolData', data)
    } else {
      const { data } = await getSchoolAPI()
      if (data && Array.isArray(data)) {
        commit('setSchoolData', data)
      }
    }
  },
  async getUserInfo({ commit }, params) {
    const { data } = await getUserInfoAPI(params)
    if (data) {
      commit('setUserIfno', data.user)
      commit('SET_SIGN', true)
      return data
    }
  },
  async setQiniuToken({ commit }, params) {
    const { data } = await getQiniuToken(params)
    if (data) {
      commit('QINIU_TOKEN', data)
      return data
    }
  },
  setQiniuUpdate({ commit }, file) {
    return new Promise((resolve, reject) => {
      const observer = {
        next(res) {},
        error(err) {
          reject(err)
        },
        complete(res) {
          const fileUrl = `https://wormhole.icu/` + res.key
          resolve(fileUrl)
        },
      }
      const observable = qiniu.upload(
        file,
        null,
        store.getters['baseStore/qiniuToken'],
        {},
        {}
      )
      observable.subscribe(observer)
    })
  },
}
export default { state, getters, mutations, actions }
